package juc.base;

public class IthreadJoin {
    static int num = 0;

    public static void main(String[] args) throws Exception {
    }

    /*线程按照顺序执行*/
    private static void threadOrder() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("t1");
        });
        Thread t2 = new Thread(() -> {
            try {
                t1.join();
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("t2");
        });
        Thread t3 = new Thread(() -> {
            try {
                t2.join();
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("t2");
        });
        t1.start();
        t2.start();
        t3.start();
        t3.join();
    }

    /*主线程在从线程执行后继续执行*/
    private static void extracted() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(1000);
                num = 5;
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        t1.start();
//        t1在此处加入主线程，t1执行结束后继续执行主线程
        t1.join();
        System.out.println(num);
    }
}